<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-tree :data="syTypeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id"
            highlight-current @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <div class="app-container-table-border">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="0px" class="top-left">
            <el-form-item label prop="excelTypeName">
              <el-input v-model="queryParams.excelTypeName" placeholder="请输入表单类型" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label prop="label" label-width="40px">
              <el-input v-model="queryParams.label" placeholder="请输入简称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
            <el-col :span="1.5">
              <el-button type="info" plain icon="el-icon-download" size="mini" :disabled="multiple" @click="handlePrint"
                v-hasPermi="['syrw:biExcelType:print']">打印</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['syrw:biExcelType:add']">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
              @click="handleUpdate" v-hasPermi="['syrw:biExcelType:edit']">修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
              @click="handleDelete" v-hasPermi="['syrw:biExcelType:remove']">删除
            </el-button>
            </el-col>-->
            <!-- <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" :disabled="multiple"
                @click="handleExport" v-hasPermi="['syrw:biExcelType:export']">导出</el-button>
            </el-col> -->
            <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
          </el-row>

          <el-table border stripe v-loading="loading" :data="biExcelTypeList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!--          <el-table-column label="试验记录类型id" align="center" prop="excelTypeId" v-if="true"/>-->
            <!--          <el-table-column label="归属试验类型id" align="center" prop="syTypeId"/>-->
            <el-table-column label="表单类型" fixed align="center" prop="excelTypeName" min-width="200"
              show-overflow-tooltip />
            <el-table-column label="简称" align="center" prop="label" show-overflow-tooltip min-width="120" />
            <el-table-column label="每份默认页数" align="center" prop="defaultNum" show-overflow-tooltip min-width="120" />
            <el-table-column label="排序" align="center" prop="typeSort" show-overflow-tooltip min-width="120" />
            <!--          <el-table-column label="对应导出的excel模板路径存放在后端代码内" align="center" prop="excelPath"-->
            <!--            min-width="180" show-overflow-tooltip />-->
            <!--          <el-table-column label="前端组件路径" align="center" prop="compontPath" min-width="180"-->
            <!--            show-overflow-tooltip />-->
            <!--          <el-table-column label="导出时文件名或者sheet名" align="center" prop="exportFileName"-->
            <!--            min-width="180" show-overflow-tooltip />-->
            <!--          <el-table-column label="扩展属性" align="center" prop="expandInfo" min-width="180"-->
            <!--            show-overflow-tooltip />-->
            <el-table-column v-if="checkPermi(['syrw:biExcelType:edit', 'syrw:biExcelType:remove'])" label="操作"
              align="center" class-name="small-padding fixed-width" width="160px">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['syrw:biExcelType:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['syrw:biExcelType:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-col>
    </el-row>
    <!-- 添加或修改Excel类型管理对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属试验类型" prop="syTypeId">
          <el-cascader v-model="form.syTypeId" :options="syTypeList" :show-all-levels="false"
            :props="{ expandTrigger: 'hover', value: 'id', emitPath: false }" @change="handleChange"
            style="width: 100%;"></el-cascader>
        </el-form-item>
        <el-form-item label="试验记录类型名称" prop="excelTypeName" class="narrow-spacing">
          <el-input v-model="form.excelTypeName" placeholder="请输入试验记录类型名称" />
        </el-form-item>
        <el-form-item label="简称" prop="label">
          <el-input v-model="form.label" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="每份默认页数" prop="defaultNum">
          <el-input-number v-model="form.defaultNum" placeholder="请输入每份默认页数" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="排序" prop="typeSort">
          <el-input-number v-model="form.typeSort" placeholder="请输入排序" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="对应导出的excel模板路径存放在后端代码内" prop="excelPath" class="narrow-spacing">
          <el-input v-model="form.excelPath" placeholder="请输入对应导出的excel模板路径存放在后端代码内" />
        </el-form-item>
        <el-form-item label="前端组件路径" prop="compontPath">
          <el-input v-model="form.compontPath" placeholder="请输入前端组件路径" />
        </el-form-item>
        <el-form-item label="导出时文件名或者sheet名" prop="exportFileName" class="narrow-spacing">
          <el-input v-model="form.exportFileName" placeholder="请输入导出时文件名或者sheet名" />
        </el-form-item>
        <el-form-item label="扩展属性" prop="expandInfo">
          <el-input v-model="form.expandInfo" placeholder="请输入扩展属性" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
    <statement-select-drawer ref="statementSelectDrawer" />
    <statement-select-dialog ref="statementSelectDialog" />
  </div>
</template>

<script>
import { listBiExcelType, getBiExcelType, delBiExcelType, addBiExcelType, updateBiExcelType } from "@/api/syrw/biExcelType";
import { syTpyeTree } from "@/api/base/syType";
import { checkPermi } from "@/utils/permission";
import StatementSelectDialog from "@/views/reportForms/components/StatementSelectDialog.vue";
import StatementSelectDrawer from "@/views/reportForms/components/StatementSelectDrawer.vue";
export default {
  name: "BiExcelType",
  components: {
    StatementSelectDialog,
    StatementSelectDrawer
  },
  data () {
    return {
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 试验类型列表
      syTypeList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Excel类型管理表格数据
      biExcelTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        syTypeId: undefined,
        excelTypeName: undefined,
        label: undefined,
        excelPath: undefined,
        compontPath: undefined,
        exportFileName: undefined,
        expandInfo: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        excelTypeId: [{ required: true, message: "试验记录类型id不能为空", trigger: "blur" }],
        syTypeId: [{ required: true, message: "归属试验类型不能为空", trigger: "change" }],
        excelTypeName: [{ required: true, message: "试验记录类型名称不能为空", trigger: "blur" }],
        label: [{ required: true, message: "简称不能为空", trigger: "blur" }],
        typeSort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        defaultNum: [{ required: true, message: "每份默认页数不能为空", trigger: "blur" }],
        excelPath: [
          // { required: true, message: "对应导出的excel模板路径存放在后端代码内不能为空", trigger: "blur" }
        ],
        compontPath: [
          // { required: true, message: "前端组件路径不能为空", trigger: "blur" }
        ],
        exportFileName: [
          // { required: true, message: "导出时文件名或者sheet名不能为空", trigger: "blur" }
        ],
        expandInfo: [
          // { required: true, message: "扩展属性不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
    this.getSyTypeTreeList();
  },
  methods: {
    handleChange (value) { },
    handleNodeClick (data) {
      this.queryParams.syTypeId = data.id;
      this.form.syTypeId = data.id;
      this.getList();
    },
    /** 查询试验类型树 */
    getSyTypeTreeList () {
      let param = {
        level: 2,
        innerFlag: "1"
      };
      syTpyeTree(param).then((response) => {
        this.syTypeList = response.data;
      });
    },
    /** 查询Excel类型管理列表 */
    getList () {
      this.loading = true;
      listBiExcelType(this.queryParams).then((response) => {
        this.biExcelTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        excelTypeId: undefined,
        syTypeId: undefined,
        excelTypeName: undefined,
        label: undefined,
        excelPath: undefined,
        compontPath: undefined,
        exportFileName: undefined,
        expandInfo: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.excelTypeId);
      this.selection = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加Excel类型管理";
      console.log(this.queryParams.syTypeId);
      this.form.syTypeId = this.queryParams.syTypeId;
    },

    // handlePrint () {
    //   this.$message.success("功能开发中...")
    // },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const excelTypeId = row.excelTypeId || this.ids;
      getBiExcelType(excelTypeId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改Excel类型管理";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.excelTypeId != null) {
            updateBiExcelType(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBiExcelType(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const excelTypeIds = row.excelTypeId || this.ids;
      this.$modal
        .confirm('是否确认删除Excel类型管理编号为"' + excelTypeIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delBiExcelType(excelTypeIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "syrw/biExcelType/export",
        {
          ...this.queryParams
        },
        `biExcelType_${new Date().getTime()}.xlsx`
      );
    },
    checkPermi (arr) {
      return checkPermi(arr);
    },
    //预览
    handlePrint () {
      const tableList = this.selection;
      if (!tableList || tableList.length <= 0) {
        return;
      }
      const arr = tableList.map((item) => {
        return { label: item.label, syType: this.queryParams.syTypeId, typeSort: item.typeSort };
      });
      this.$refs.statementSelectDrawer.init(arr);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/demo.scss";
</style>
